package com.example.servlet;

import com.alibaba.fastjson.JSON;
import com.example.dao.CommentDao;
import com.example.dao.PostDao;
import com.example.dao.UserDao;
import com.example.dao.imp.CommentDaoImp;
import com.example.dao.imp.PostDaoImp;
import com.example.dao.imp.UserDaoImp;
import com.example.domain.Comment;
import com.example.domain.Post;
import com.example.domain.Users;
import com.example.utils.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@WebServlet(name = "CommentServlet", value = "/commentServlet")
public class CommentServlet extends HttpServlet {

    private CommentDao commentDao = new CommentDaoImp();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String postId = request.getParameter("postId");
        if (postId != null) {
            int postID = Integer.parseInt(postId);
            List<Comment> commentList = commentDao.getCommentsByPostId(postID);
            ResponseUtil.sendSuccessResponse(response, 200, commentList);
        } else {
            ResponseUtil.sendErrorResponse(response, 400, "Post ID is required");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取表单提交的内容
        String content = request.getParameter("content");
        String postIdStr = request.getParameter("reid");

        // 验证内容和帖子ID
        if (content != null && !content.isEmpty() && postIdStr != null && !postIdStr.isEmpty()) {
            int postId = Integer.parseInt(postIdStr);

            // 获取当前用户ID，假设从会话中获取
            HttpSession session = request.getSession();
            Users currentUser = (Users) session.getAttribute("user");
            if (currentUser == null) {
                ResponseUtil.sendErrorResponse(response, 401, "用户未登录");
                return;
            }
            // 创建新的评论对象
            Comment comment = new Comment();
            comment.setContent(content);
            comment.setPostId(postId);
            comment.setUserId(currentUser.getId());
            comment.setCreateTime(LocalDateTime.now());

            // 将评论保存到数据库
            CommentDao commentDao = new CommentDaoImp();
            commentDao.addComment(comment);

            // 调用 PostDao 获取帖子信息
            PostDao postDao = new PostDaoImp();
            Post post = postDao.getPostById(postId);
            UserDao userDao = new UserDaoImp();
            Users postUser = userDao.getUsersByUserId(post.getUserId());
            post.setUser(postUser);

            // 调用 CommentDao 获取帖子下的评论列表
            List<Comment> commentList = commentDao.getCommentsByPostId(postId);
            for (Comment c : commentList) {
                Users user = userDao.getUsersByUserId(c.getUserId());
                c.setUser(user);
            }

            // 将帖子信息和评论列表存入 request 中
            request.setAttribute("post", post);
            request.setAttribute("commentList", commentList);

            // 转发到 JSP 页面进行渲染
            request.getRequestDispatcher("/postInfo.jsp").forward(request, response);
        } else {
            ResponseUtil.sendErrorResponse(response, 400, "内容或帖子ID不能为空");
        }
    }


    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        if (id != null) {
            int commentId = Integer.parseInt(id);
            commentDao.deleteComment(commentId);
            ResponseUtil.sendSuccessResponse(response, 200, "Comment deleted successfully");
        } else {
            ResponseUtil.sendErrorResponse(response, 400, "Comment ID is required");
        }
    }
}
